/****************************************************************
 *
 * Project: Handle ARM M4 STM32F407VG In 2 Weeks
 * 
 ****************************************************************/

/****************************************************************
 *
 *	Module		: "header file" mySpi.h
 *  Tool		: KeilC ARM 4.22a
 *	Chip		: STM32F407VG
 * 	History		: 9/12/2011
 *	Description	: Day 7
				  + My library to exchange data via Spi peripheral
				  + Reference to header file "mySpi.c"
 *				
 *	Author		: Dang Anh Tung			
 *	Description	: www.payitforward.edu.vn
 *				
 *
 ****************************************************************/

 
 /****************************************************************
 * 							IMPORT
 ****************************************************************/
 #include "stm32f4xx.h"


/*****************************************************************
* 							DEFINE
******************************************************************/

#define DMA_SPI1_TX_STREAM		DMA2_Stream3	
#define DMA_SPI1_TX_CHANNEL		DMA_Channel_3
#define	DMA_SPI1_TX_PERIPH		(uint32_t)SPI1 + 0x0C	  /*SPI Data register address*/
#define DMA_SPI1_TX_PRIORITY	DMA_Priority_High
#define DMA_SPI1_TX_STREAM_IRQ	DMA2_Stream3_IRQn

#define DMA_SPI1_RX_STREAM		DMA2_Stream2	
#define DMA_SPI1_RX_CHANNEL		DMA_Channel_3
#define	DMA_SPI1_RX_PERIPH		(uint32_t)SPI1 + 0x0C
#define DMA_SPI1_RX_PRIORITY	DMA_Priority_High
#define DMA_SPI1_RX_STREAM_IRQ	DMA2_Stream2_IRQn

#define DMA_SPI2_TX_STREAM		DMA1_Stream4	
#define DMA_SPI2_TX_CHANNEL		DMA_Channel_0
#define	DMA_SPI2_TX_PERIPH		(uint32_t)SPI2 + 0x0C
#define DMA_SPI2_TX_PRIORITY	DMA_Priority_High
#define DMA_SPI2_TX_STREAM_IRQ	DMA1_Stream3_IRQn

#define DMA_SPI2_RX_STREAM		DMA1_Stream3	
#define DMA_SPI2_RX_CHANNEL		DMA_Channel_0
#define	DMA_SPI2_RX_PERIPH		(uint32_t)SPI2 + 0x0C
#define DMA_SPI2_RX_PRIORITY	DMA_Priority_High
#define DMA_SPI2_RX_STREAM_IRQ	DMA1_Stream3_IRQn

#define DMA_SPI3_TX_STREAM		DMA1_Stream5	
#define DMA_SPI3_TX_CHANNEL		DMA_Channel_0
#define	DMA_SPI3_TX_PERIPH		(uint32_t)SPI2 + 0x0C
#define DMA_SPI3_TX_PRIORITY	DMA_Priority_High
#define DMA_SPI3_TX_STREAM_IRQ	DMA1_Stream5_IRQn

#define DMA_SPI3_RX_STREAM		DMA1_Stream2	
#define DMA_SPI3_RX_CHANNEL		DMA_Channel_0
#define	DMA_SPI3_RX_PERIPH		(uint32_t)SPI2 + 0x0C
#define DMA_SPI3_RX_PRIORITY	DMA_Priority_High
#define DMA_SPI3_RX_STREAM_IRQ	DMA1_Stream2_IRQn




#define CTRL_REG1_ADDR			(uint8_t)0x20
#define CTRL_REG2_ADDR			(uint8_t)0x21
#define CTRL_REG3_ADDR			(uint8_t)0x22
#define WHO_AM_I_ADDR			(uint8_t)0x0F
#define STATUS_ADDR				(uint8_t)0x27
#define OUT_X_ADDR				(uint8_t)0x29
#define OUT_Y_ADDR				(uint8_t)0x2B
#define OUT_Z_ADDR				(uint8_t)0x2D

#define WRITE_UNCHANGE_ADDR		(uint8_t)0x00
#define WRITE_INCREMENT_ADDR 	(uint8_t)0x40
#define READ_UNCHANGE_ADDR		(uint8_t)0x80
#define READ_INCREMENT_ADDR		(uint8_t)0xC0	
#define CTRL_REG1_FIRST_TIME	(uint8_t)0x47
#define CTRL_REG2_FIRST_TIME	(uint8_t)0x00			//0X40: REBOOT
#define CTRL_REG3_FIRST_TIME	(uint8_t)0x00

#define DUMMY_BYTE				(uint8_t)0x00

 /****************************************************************
 * 							GLOBAL
 ****************************************************************/


 /****************************************************************
 * 						   FUNCTIONS
 ****************************************************************/
//	Turn on LIS302 sensor
void myLis302PowerUp(void);

//	Init all SPI that wil be used in IMU
void mySpiInit(void);

//	Init all DMA SPI that will be used
void myDmaSpiInit(void);

//	Init all DMA Interrupt that will be used
void myDmaSpiInterruptInit(void);

//  Exchange 1 byte with slave : transmitt 1 byte & receive 1 byte  
uint8_t mySpiExchangeByte(SPI_TypeDef* SPIx, uint8_t byte);

//	Read multi-bytes (numByte) continuously begin at start address in SPI slave by the NORMAL BUS
void mySpiReadBytes(SPI_TypeDef* SPIx, uint8_t *readBuffer, uint8_t startAddr, uint32_t numByte);

//	Write multi-byte (numByte) continuously begin at start address in SPI slave	by the NORMAL BUS
void mySpiWriteBytes(SPI_TypeDef* SPIx, uint8_t *writeBuffer, uint8_t startAddr, uint32_t numByte);

//	Read multi-bytes (numByte) continuously begin at start address in SPI slave by the DMA BUS
void mySpiReadDma(DMA_Stream_TypeDef* DMA_StreamRx,DMA_Stream_TypeDef* DMA_StreamTx, SPI_TypeDef* SPIx, uint8_t *readBuffer, uint8_t startAddr, uint32_t numByte);

// Reserve function :)	Write multi-byte (numByte) continuously begin at start address in SPI slave	by the DMA BUS
// void mySpiWriteDma(uint8_t *writeBuffer, uint8_t startAddr, uint32_t numByte);


 /****************************************************************
 * 						   FUNCTIONS FOR SD CARD
 ****************************************************************/
 void SPI_CS_Low(void);
 void SPI_CS_High(void);
 void SD_SPI_Init(void);
 uint8_t SPI_RecvByte(void);
 uint8_t SPI_SendByte (uint8_t data);




